package com.md.api.controller;

import com.md.api.annotation.NoRepeatSubmit;
import com.md.api.common.utils.StringUtils;
import com.md.api.entity.*;
import com.md.api.entity.dto.ComposeCommitDto;
import com.md.api.entity.dto.ComposeQueryDto;
import com.md.api.entity.dto.SkuDetailDto;
import com.md.api.service.ShoppingCartService;
import com.md.api.service.ShoppingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RequestMapping("/shopping")
@RestController
public class ShoppingController {


	@Resource
	ShoppingService shoppingService;

	@Resource
	ShoppingCartService cartSevice;

	@RequestMapping("/page")
	@ResponseBody
	public HttpResult page(HttpServletRequest request,@RequestBody ComposeQueryDto qto) {

		return shoppingService.page(request,qto);
	}

	//推荐产品
	@RequestMapping("/recommend")
	@ResponseBody
	public HttpResult recommend() {

		return shoppingService.recommend();
	}


	//推荐产品
	@RequestMapping("/getNew")
	@ResponseBody
	public HttpResult getNew() {

		return shoppingService.getNew();
	}

	@RequestMapping("/spuDetail")
	@ResponseBody
	public HttpResult detail(@RequestBody ShoppingSpu spu) {

		return shoppingService.spuDetail(spu);
	}

	@RequestMapping("/skuDetail")
	@ResponseBody
	public HttpResult skuDetail(HttpServletRequest request,@RequestBody SkuDetailDto dto) {

		return shoppingService.skuDetail(request,dto);
	}


	@RequestMapping("/spuTypePage")
	@ResponseBody
	public HttpResult spuTypePage(@RequestBody  ComposeQueryDto qto) {

		return shoppingService.spuTypePage(qto);
	}

	//加入购物车
	@NoRepeatSubmit
	@RequestMapping("/addShoppingCart")
	@ResponseBody
	public HttpResult addShoppingCart(HttpServletRequest request,@RequestBody ShoppingCart shoppingCart) {
		if (null==shoppingCart.getNumCount()|| StringUtils.isEmpty(shoppingCart.getSpuId())||StringUtils.isEmpty(shoppingCart.getSkuId())){
			return HttpResult.PARAM_ERROR;
		}
		return shoppingService.addShoppingCart(request,shoppingCart);
	}


	//物流下拉选项
	@RequestMapping("/findLogistics")
	@ResponseBody
	public HttpResult findLogistics(HttpServletRequest request,@RequestBody ComposeQueryDto queryDto) {
		return shoppingService.findLogistics(queryDto);
	}



	//直接下单获取产品总价及积分
	@NoRepeatSubmit
	@RequestMapping("/trade")
	@ResponseBody
	public HttpResult trade(HttpServletRequest request,@RequestBody ComposeCommitDto qto) {
		if (null==qto.getNumCount()||StringUtils.isEmpty(qto.getSkuId())) {
			return HttpResult.PARAM_ERROR;
		}
		return cartSevice.shoppingTrade(request,qto);
	}


	//商城直接下单
	@NoRepeatSubmit
	@RequestMapping("/order")
	@ResponseBody
	public HttpResult order(HttpServletRequest request,@RequestBody ComposeCommitDto qto) {
		try {
			if (StringUtils.isEmpty(qto.getReceivingAddrId())||null==qto.getNumCount()||StringUtils.isEmpty(qto.getSkuId())) {
				return HttpResult.PARAM_ERROR;
			}
			return cartSevice.shoppingOrder(request, qto);
		}catch (Exception e) {
			e.printStackTrace();
			return HttpResult.fail("下单失败："+e.getMessage());
		}
	}
	@RequestMapping("/evaluatePage")
	@ResponseBody
	public HttpResult evaluatePage(HttpServletRequest request,@RequestBody ComposeQueryDto qto) {

		return cartSevice.evaluatePage(qto);
	}

	@RequestMapping("/evaluate")
	@ResponseBody
	public HttpResult evaluate(HttpServletRequest request,@RequestBody ShoppingSpuEvaluate evaluate) {

		if (StringUtils.isEmpty(evaluate.getOrderNo())||StringUtils.isEmpty(evaluate.getMsg())||null==evaluate.getStarNum()){
			return HttpResult.PARAM_ERROR;
		}
		return cartSevice.evaluate(request,evaluate);
	}
}
